package com.ruoyi.project.mobile.user;

import com.ruoyi.common.utils.*;
import com.ruoyi.common.utils.uniquekey.IdGererateFactory;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.coin.domain.Coin;
import com.ruoyi.project.system.coin.dto.CoinDto;
import com.ruoyi.project.system.coin.service.ICoinService;
import com.ruoyi.project.system.coinrecord.domain.HaoqiCoinRecord;
import com.ruoyi.project.system.coinrecord.service.IHaoqiCoinRecordService;
import com.ruoyi.project.system.coupon.usercoupon.domain.UserCoupon;
import com.ruoyi.project.system.coupon.usercoupon.service.IUserCouponService;
import com.ruoyi.project.system.donation.domain.MemberDonation;
import com.ruoyi.project.system.donation.service.IMemberDonationService;
import com.ruoyi.project.system.feedback.domain.Feedback;
import com.ruoyi.project.system.feedback.service.IFeedbackService;
import com.ruoyi.project.system.goods.follow.dto.GoodsFollowDto;
import com.ruoyi.project.system.goods.follow.service.IGoodsFollowService;
import com.ruoyi.project.system.goods.goods.service.IGoodsGoodsService;
import com.ruoyi.project.system.goods.img.domain.GoodsImg;
import com.ruoyi.project.system.goods.img.service.IGoodsImgService;
import com.ruoyi.project.system.history.domain.MemberGoodsHistory;
import com.ruoyi.project.system.history.service.IMemberGoodsHistoryService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.enums.MemberExamine;
import com.ruoyi.project.system.member.service.IMemberService;
import com.ruoyi.project.system.membershare.domain.MemberShare;
import com.ruoyi.project.system.membershare.service.IMemberShareService;
import com.ruoyi.project.system.order.order.dto.OrderOrderDto;
import com.ruoyi.project.system.order.order.enums.OrderStatus;
import com.ruoyi.project.system.order.order.service.IOrderOrderService;
import com.ruoyi.project.system.shopconfig.domain.ShopConfig;
import com.ruoyi.project.system.shopconfig.service.IShopConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * 功能：
 *
 * @author：dell
 * @create：2020/6/24 16:25
 * @version：2020 Version：1.0
 * @company：老屋新房 Created with IntelliJ IDEA
 */
@RestController
@RequestMapping("/api/user")
public class ApiUserController extends BaseController {

	@Autowired
	private IMemberService memberService;

	@Autowired
	private IOrderOrderService orderOrderService;

	@Autowired
	private IGoodsGoodsService goodsGoodsService;

	@Autowired
	private IGoodsFollowService goodsFollowService;
	@Autowired
	private ServerConfig serverConfig;
	@Autowired
	private IFeedbackService feedbackService;
	@Autowired
	private IShopConfigService shopConfigService;
	@Autowired
	private IMemberGoodsHistoryService memberGoodsHistoryService;

    @Autowired
    private IHaoqiCoinRecordService haoqiCoinRecordService;
    @Autowired
    private IMemberShareService memberShareService;
    @Autowired
    private IdGererateFactory idGererateFactory;

	@Autowired
	private IUserCouponService userCouponService;

	@Autowired
	private IGoodsImgService goodsImgService;

	@Autowired
	private ICoinService coinService;

	@Autowired
	private IMemberDonationService memberDonationService;

	/**
	 * 点击会员中心 查询用户信息
	 * @param uid
	 * @return
	 */
	@GetMapping
	public R findUser(@RequestParam String uid){
		HashMap<String, Object> map = new HashMap<>();

		Member oldMember = this.memberService.selectMemberById(uid);


		if(oldMember==null){
			return R.ok();
		}


		map.put("user",oldMember);


		UserCoupon userCoupon = new UserCoupon();
		userCoupon.setMemberId(uid);
		userCoupon.setStatus(0);
		userCoupon.setOverdue(0);
		map.put("couponNum",this.userCouponService.selectUserCouponList(userCoupon).size());
		MemberGoodsHistory memberGoodsHistory = new MemberGoodsHistory();
		memberGoodsHistory.setMemberId(uid);
		map.put("history",this.memberGoodsHistoryService.selectMemberGoodsHistoryList(memberGoodsHistory).size());
		//待支付订单数量
		Integer[] toPaid = {OrderStatus.TO_BE_PAID.getValue()};
		map.put("toPayNum",this.orderOrderService.findListByStatusAndUid(toPaid,uid,0).size());
		//待发货订单数量
		Integer[] toDelivered = {OrderStatus.TO_BE_DELIVERED.getValue(),OrderStatus.CUSTOMS.getValue()};
		map.put("toDelivered",this.orderOrderService.findListByStatusAndUid(toDelivered,uid,0).size());
		//待收货订单数量
		Integer[] toReceived = {OrderStatus.TO_BE_RECEIVED.getValue()};
		map.put("toReceived",this.orderOrderService.findListByStatusAndUid(toReceived,uid,0).size());
		//待评论订单数量
		Integer[] toComment = {OrderStatus.TO_BE_COMMENT.getValue()};
		map.put("toComment",this.orderOrderService.findListByStatusAndUid(toComment,uid,0).size());
		//退款售后订单数量
//		Integer[] rxOrder = {OrderStatus.TO_BE_RETURN.getValue(),OrderStatus.TO_BE_EXCHANGE.getValue()};
		map.put("rxOrder",this.orderOrderService.findListByStatusAndUid(null,uid,1).size());
		return RFactory.generateR(map);
	}

	/**
	 * 查询关注列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/follows")
	public R userFollows(@RequestParam String uid){
		startPage();
		List<GoodsFollowDto> goodsFollowDtoList = this.goodsFollowService.findListByUid(uid);
		return RFactory.generateR(getDataTable(goodsFollowDtoList));
	}

	/**
	 * 查询用户详细信息
	 * @param uid
	 * @return
	 */
	@GetMapping("/info")
	public R userInfo(@RequestParam String uid){
		return RFactory.generateR(this.memberService.selectMemberById(uid));
	}



	/**
	 * 修改用户信息
	 * @param map
	 * @return
	 */
	@PutMapping("/info")
	public R updateUserInfo(@RequestBody Map map){

		String uid = (String) map.get("uid");
		String name = (String) map.get("name");
		String idCard = (String) map.get("idCard");
		String mobile = (String) map.get("mobile");
		String header = (String) map.get("headerImg");
		String back = (String) map.get("backImg");
        String headpic = (String) map.get("headpic");
		String birthday = (String) map.get("birthday");
        int sex = (int) map.get("sex");
        String username = (String) map.get("username");
		Member member = new Member();
		member.setId(uid);
		member.setName(name);
		member.setIdCard(idCard);
		member.setMobile(mobile);
//		member.setIsExamine(MemberExamine.ADOPT.getValue());
		member.setCardHeaderImg(header);
		member.setHeadpic(headpic);
		member.setSex(sex);
		member.setUsername(username);
		//上传身份证背面的图片
		member.setCardBackImg(back);
		member.setBirthday(birthday);
		this.memberService.updateMember(member);

        Member memberInfo = this.memberService.selectMemberById((String)map.get("uid"));
        if (memberInfo.getIsPerInfo()==null) {
            //  判断用户头像、昵称、姓名、性别、生日、手机号是否为空
            if (!StringUtils.isBlank((String) map.get("name"))
                    && !StringUtils.isBlank((String) map.get("mobile"))
                    && !StringUtils.isBlank((String) map.get("username"))
                    && !StringUtils.isBlank((String) map.get("headpic"))
//                    && !StringUtils.isBlank((String) map.get("sex"))
                    && map.get("sex") !=null
            ) {
//            Member memberInfo = this.memberService.selectMemberById((String)map.get("uid"));
                //  用户积分增加
                memberInfo.setHaoqiCoin(memberInfo.getHaoqiCoin().add(new BigDecimal(30)));
                //  设置历史总积分
                memberInfo.setHistoryTotalCoin(memberInfo.getHistoryTotalCoin().add(new BigDecimal(30)));
                memberInfo.setIsPerInfo(1);
                this.memberService.updateMember(memberInfo);

                //完善会员信息进行积分反馈
                HaoqiCoinRecord coinRecord = new HaoqiCoinRecord();
                coinRecord.setStatus(1);
                coinRecord.setMemberId(memberInfo.getId());
                coinRecord.setType(2);
                coinRecord.setGetType(5);
                coinRecord.setCreated(DateUtils.getNowDate());
                coinRecord.setNum(new BigDecimal(30));
                coinRecord.setResId(memberInfo.getId());
                this.haoqiCoinRecordService.insertHaoqiCoinRecord(coinRecord);
            }
        }


		return R.ok();
	}


	/**
	 * 查询推荐人列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/recommend")
	public R userRecommend(@RequestParam String uid){
		startPage();
		Member member = new Member();
		member.setIntroUser(uid);
		List<Member> members = this.memberService.seMemberList(uid);
		return RFactory.generateR(getDataTable(members));
	}

	/**
	 * 修改用户信息
	 * @param map
	 * @return
	 */
	@PutMapping("/info2")
	public R updateUserInfo2(@RequestBody Map map){

        Member memberInfo = this.memberService.selectMemberById((String)map.get("id"));

		String username = (String) map.get("username");
		String id = (String) map.get("id");
		int sex = (int) map.get("sex");
		String headpic = (String) map.get("headpic");
		Member member = new Member();
		member.setId(id);
		member.setUsername(username);
		member.setSex(sex);
		member.setHeadpic(headpic);
		this.memberService.updateMember(member);

        if (memberInfo.getIsPerInfo()==null) {
            //  判断用户头像、昵称、姓名、性别、生日、手机号是否为空
            if (!StringUtils.isBlank(memberInfo.getHeadpic())
                    && !StringUtils.isBlank(memberInfo.getUsername())
                    && !StringUtils.isBlank(memberInfo.getName())
                    && memberInfo.getSex() !=null
                    && !StringUtils.isBlank(memberInfo.getMobile())) {
                //  用户积分增加
                memberInfo.setHaoqiCoin(memberInfo.getHaoqiCoin().add(new BigDecimal(30)));
                //  设置历史总积分
                memberInfo.setHistoryTotalCoin(memberInfo.getHistoryTotalCoin().add(new BigDecimal(30)));
                memberInfo.setIsPerInfo(1);
                this.memberService.updateMember(memberInfo);

                //完善会员信息进行积分反馈
                HaoqiCoinRecord coinRecord = new HaoqiCoinRecord();
                coinRecord.setStatus(1);
                coinRecord.setMemberId(memberInfo.getId());
                coinRecord.setType(2);
                coinRecord.setGetType(5);
                coinRecord.setCreated(DateUtils.getNowDate());
                coinRecord.setNum(new BigDecimal(30));
                coinRecord.setResId(memberInfo.getId());
                this.haoqiCoinRecordService.insertHaoqiCoinRecord(coinRecord);
            }
        }


		return R.ok();
	}


	/**
	 * 提交意见
	 * @param feedback
	 * @return
	 */
	@PostMapping("/feedback")
	public R userFeedback(@RequestBody Feedback feedback){
		feedback.setCreated(DateUtils.getNowDate());
		this.feedbackService.insertFeedback(feedback);
		return R.ok();
	}

	@GetMapping("/share")
	public R share(@RequestParam String uid){
		HashMap<String, Object> map = new HashMap<>();
		Member member = this.memberService.selectMemberById(uid);
		if(StringUtils.isBlank(member.getQrCode())){
			String qrCode = WeixinUtils.getQrCodeImg(member);
			member.setQrCode(qrCode);
			Member update = new Member();
			update.setQrCode(qrCode);
			update.setId(uid);
			this.memberService.updateMember(update);
		}
		map.put("avator",member.getHeadpic());
		map.put("name",member.getUsername());
		map.put("InvitationCode","");
		map.put("shopId","");
		map.put("imgurl",java.util.Arrays.asList(this.shopConfigService.findAll().get(0).getShareImg()));
		map.put("qrCode",member.getQrCode());
		return RFactory.generateR(map);
	}


	/**
	 * 查询足迹列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/history")
	public R userHistoryList(@RequestParam String uid){
		//获取日期
		this.startPage();
		List<String> dayList = this.memberGoodsHistoryService.findDayList(uid);
		//通过日期查询商品列表
		List data = new ArrayList();
		for(String day:dayList){
			HashMap<String, Object> map = new HashMap<>();
			map.put("day",day);
			map.put("goodsList",this.memberGoodsHistoryService.findGoodsListByUidAndDay(uid,day));
			data.add(map);
		}
		TableDataInfo dataTable = getDataTable(dayList);
		dataTable.setRows(data);
		return RFactory.generateR(dataTable);
	}

    /**
     * 用户分享商品获得积分
     * @return
     */
    @PostMapping("/shareGoods")
    @Transactional
    public R shareGoods(@RequestBody MemberShare memberShare){
        //查询用户分享信息
        MemberShare byMemberId = this.memberShareService.selectMemberShareByMemberId(memberShare.getMemberId());
        if (byMemberId == null) {
            //生成分享记录
            MemberShare userShare = new MemberShare();
            userShare.setId(idGererateFactory.nextStringId());
            userShare.setMemberId(memberShare.getMemberId());
            userShare.setTotal(1);
            userShare.setShareNum(1);
            userShare.setUpdateTime(DateUtils.getNowDate());
            this.memberShareService.insertMemberShare(userShare);

            //生成积分记录
            HaoqiCoinRecord haoqiCoinRecord = new HaoqiCoinRecord();
            haoqiCoinRecord.setNum(new BigDecimal(10));
            haoqiCoinRecord.setCreated(DateUtils.getNowDate());
            haoqiCoinRecord.setStatus(1);
            haoqiCoinRecord.setType(2);
            haoqiCoinRecord.setMemberId(memberShare.getMemberId());
            haoqiCoinRecord.setResId(memberShare.getMemberId());
            haoqiCoinRecord.setGetType(3);
            this.haoqiCoinRecordService.insertHaoqiCoinRecord(haoqiCoinRecord);

            //用户积分变更
            Member member = this.memberService.selectMemberById(memberShare.getMemberId());

            Member update = new Member();
            update.setId(memberShare.getMemberId());
            update.setHaoqiCoin(member.getHaoqiCoin().add(new BigDecimal(10)));
            update.setHistoryTotalCoin(member.getHistoryTotalCoin().add(new BigDecimal(10)));
            this.memberService.updateMember(update);
        } else

        //判断更新时间不为空
        if (byMemberId.getUpdateTime() != null ) {
            //判断更新时间不是今天
            if (!DateUtils.IsToday(byMemberId.getUpdateTime())) {
                MemberShare noTodayMemberShare = this.memberShareService.selectMemberShareByMemberId(memberShare.getMemberId());
                //生成分享记录
                noTodayMemberShare.setShareNum(1);
                noTodayMemberShare.setTotal(byMemberId.getTotal()+1);
                noTodayMemberShare.setUpdateTime(DateUtils.getNowDate());
                this.memberShareService.updateMemberShare(noTodayMemberShare);
                //生成积分记录
                HaoqiCoinRecord haoqiCoinRecord = new HaoqiCoinRecord();
                haoqiCoinRecord.setNum(new BigDecimal(10));
                haoqiCoinRecord.setCreated(DateUtils.getNowDate());
                haoqiCoinRecord.setStatus(1);
                haoqiCoinRecord.setType(2);
                haoqiCoinRecord.setMemberId(memberShare.getMemberId());
                haoqiCoinRecord.setResId(memberShare.getMemberId());
                haoqiCoinRecord.setGetType(3);
                this.haoqiCoinRecordService.insertHaoqiCoinRecord(haoqiCoinRecord);

                //用户积分变更
                Member member = this.memberService.selectMemberById(memberShare.getMemberId());

                Member update = new Member();
                update.setId(memberShare.getMemberId());
                update.setHaoqiCoin(member.getHaoqiCoin().add(new BigDecimal(10)));
                update.setHistoryTotalCoin(member.getHistoryTotalCoin().add(new BigDecimal(10)));
                this.memberService.updateMember(update);
            } else
            //判断今天分享是否超过三次
            if (DateUtils.IsToday(byMemberId.getUpdateTime())
                    && byMemberId.getShareNum() < 3) {
                MemberShare todayMemberShare = this.memberShareService.selectMemberShareByMemberId(memberShare.getMemberId());
                todayMemberShare.setShareNum(todayMemberShare.getShareNum()+1);
                todayMemberShare.setTotal(todayMemberShare.getTotal()+1);
                todayMemberShare.setUpdateTime(DateUtils.getNowDate());
                this.memberShareService.updateMemberShare(todayMemberShare);

                //生成积分记录
                HaoqiCoinRecord haoqiCoinRecord = new HaoqiCoinRecord();
                haoqiCoinRecord.setNum(new BigDecimal(10));
                haoqiCoinRecord.setCreated(DateUtils.getNowDate());
                haoqiCoinRecord.setStatus(1);
                haoqiCoinRecord.setType(2);
                haoqiCoinRecord.setMemberId(todayMemberShare.getMemberId());
                haoqiCoinRecord.setResId(todayMemberShare.getMemberId());
                haoqiCoinRecord.setGetType(3);
                this.haoqiCoinRecordService.insertHaoqiCoinRecord(haoqiCoinRecord);

                //用户积分变更
                Member member = this.memberService.selectMemberById(memberShare.getMemberId());

                Member update = new Member();
                update.setId(memberShare.getMemberId());
                update.setHaoqiCoin(member.getHaoqiCoin().add(new BigDecimal(10)));
                update.setHistoryTotalCoin(member.getHistoryTotalCoin().add(new BigDecimal(10)));
                this.memberService.updateMember(update);

            }

        }

        return R.ok();
    }



	/**
	 * 查询优惠券列表
	 * @param uid
	 * @param overdue
	 * @return
	 */
	@GetMapping("/coupons")
	public R userCouponsList(@RequestParam String uid,@RequestParam Integer status,
							 @RequestParam(required = false) BigDecimal orderPrice,
							 @RequestParam(required = false) Integer overdue){
		startPage();
		UserCoupon userCoupon = new UserCoupon();
		userCoupon.setMemberId(uid);
		userCoupon.setStatus(status);
		userCoupon.setOverdue(overdue);
		userCoupon.setOrderPrice(orderPrice);
		return RFactory.generateR(getDataTable(this.userCouponService.selectUserCouponList(userCoupon)));
	}


	/**
	 * 查询积分规则
	 * @param uid
	 * @return
	 */
	@GetMapping("/coinrules")
	public R coinrules(@RequestParam String uid){
		HashMap<String, Object> map = new LinkedHashMap<>();
		HaoqiCoinRecord select = new HaoqiCoinRecord();
		select.setMemberId(uid);
		//查询新人积分
		select.setGetType(1);
		map.put("T1",this.haoqiCoinRecordService.selectHaoqiCoinRecordList(select).size()>0);
		//查询每日签到
		select.setGetType(2);
		select.setCreated(DateUtils.getNowDate());
		map.put("T2",this.haoqiCoinRecordService.selectHaoqiCoinRecordList(select).size()>0);
		//查询每日分享
		select.setGetType(3);
		map.put("T3",this.haoqiCoinRecordService.selectHaoqiCoinRecordList(select).size()>0);
		//购物返积分
		map.put("T4",false);
		//完善会员信息
		select.setGetType(5);
		select.setCreated(null);
		map.put("T5",this.haoqiCoinRecordService.selectHaoqiCoinRecordList(select).size()>0);
		//关联微信号
		select.setGetType(6);
		map.put("T6",this.haoqiCoinRecordService.selectHaoqiCoinRecordList(select).size()>0);
		return RFactory.generateR(map);
	}

	/**
	 * 提交申请成为代理
	 * @return
	 */
	@PostMapping("/examine")
	@Transactional
	public R userExamine(@RequestBody Map map){
		//用户主键ID
		String uid= (String) map.get("uid");
		//图片数组
		List<String> imgList= (List) map.get("imgList");
		//查询用户信息
		Member member = this.memberService.selectMemberById(uid);
		//用户不存在或者 用户不是 未完善 不通过状态 则不允许提交
		if(member==null||(member.getIsExamine()!=0&&member.getIsExamine()!=3)){
			return R.error("不允许提交");
		}
		if(StringUtils.isBlank(member.getName())) {
		    return R.error(501,"请先完善信息");
        }
		//清除原本的图片信息
		this.goodsImgService.deleteByGoodsId(uid);
		//修改用户为待审核
		Member update = new Member();
		update.setIsExamine(MemberExamine.TO_BE_REVIEWED.getValue());
		update.setId(uid);
		this.memberService.updateMember(update);
		//添加图片信息
		for(String img:imgList){
			GoodsImg goodsImg = new GoodsImg();
			goodsImg.setUrl(img);
			goodsImg.setGoodsId(uid);
			this.goodsImgService.insertGoodsImg(goodsImg);
		}
		return R.ok();
	}

	/**
	 * 查询我的团队
	 * @param uid
	 * @return
	 */
	@GetMapping("/group")
	public R myGroup(@RequestParam String uid){
		HashMap<String, Object> map = new HashMap<>();
		//查询今日收益
		BigDecimal daySum= this.coinService.findSumByType(uid,DateUtils.getNowDate(),0);
		map.put("daySum",daySum);
		//查询本月收益
		BigDecimal mouthSum= this.coinService.findSumByType(uid,DateUtils.getNowDate(),1);
		map.put("mouthSum",mouthSum);
		//查询总收益
		BigDecimal allSum= this.coinService.findSumByType(uid,DateUtils.getNowDate(),2);
		map.put("allSum",allSum);
		//查询本月业绩
		BigDecimal allOrderSum= this.coinService.findOrderSumByType(uid,DateUtils.getNowDate(),2);
		//查询本月个人订单总额
		BigDecimal sumOrder = this.orderOrderService.findSumCountByUid(uid,0);
		map.put("allOrderSum",allOrderSum.add(sumOrder));
		//团队人数
        Member byId = this.memberService.selectMemberById(uid);
        Member member = new Member();
		if(byId.getIsVip()==1) {
            member.setIntroUser(uid);
//            member.setIsVip(1);
            map.put("groupNum",this.memberService.seMemberList(uid).size());
            map.put("star",20);
            map.put("score",150000);
        } else if (byId.getIsVip()==2) {
            member.setIntroUser(uid);
//            member.setIsVip(2);
            map.put("groupNum",this.memberService.seMemberList(uid).size());
            map.put("star",10);
            map.put("score",1500000);
        }  else if (byId.getIsVip()==3) {
            member.setIntroUser(uid);
            map.put("groupNum",this.memberService.seMemberList(uid).size());
            map.put("star","");
            map.put("score","");
        }


        Coin coin = new Coin();
        coin.setMemberId(uid);
        // 得到返利记录
        List<CoinDto> coinDtoList = this.coinService.selectHaoqiCoinRecordList(coin);
        if (coinDtoList.size()>0) {
            for (Coin coinr:coinDtoList) {
                // 判断是否是返利记录
                if (coinr.getResId() != null) {
                    // 通过资源ID查订单
                    OrderOrderDto byOrderNo = this.orderOrderService.findByOrderNo(coinr.getResId());
                    if (byOrderNo != null && byOrderNo.getCloseTime() != null) {
                        // 判断确定收货时间是否过了7天
                        Date dateNow = new Date(); // 当前时间
                        long betweenDays = (dateNow.getTime() - byOrderNo.getCloseTime().getTime()) / (1000L*3600L*24L);
                        if (betweenDays > 7) {
                            // 判断返利记录状态为未到账
                            if (coinr.getStatus() == 0) {
                                // 修改返利记录状态为到账
                                Coin coin1 = new Coin();
                                coin1.setId(coinr.getId());
                                coin1.setStatus(1);
                                this.coinService.updateHaoqiCoinRecord(coin1);
                                Member memberById=this.memberService.selectMemberById(uid);
                                // 修改用户收益
                                Member user = new Member();
                                user.setId(uid);
                                user.setCoin(memberById.getCoin().add(coinr.getNum()));
                                this.memberService.updateMember(user);
                            }

                        }
                    }

                }

            }
        }

		return RFactory.generateR(map);
	}

	/**
	 * 查询收益记录
	 * @param uid
	 * @param type
	 * @return
	 */
	@GetMapping("/coin")
	public R coinList(@RequestParam String uid,@RequestParam Integer type){
		startPage();
		Coin coin = new Coin();
		coin.setType(type);
		coin.setMemberId(uid);
		return RFactory.generateR(getDataTable(this.coinService.selectHaoqiCoinRecordList(coin)));
	}

	/**
	 * 申请提现
	 * @param coin
	 * @return
	 */
	@PostMapping("/coin")
	@Transactional
	public R payCoin(@RequestBody Coin coin){
		//判断用户余额是否充足
		Member member = this.memberService.selectMemberById(coin.getMemberId());
		if(member==null){
			return R.error("用户信息错误");
		}
		if(member.getCoin().compareTo(coin.getNum())==-1){
			return R.error("余额不足");
		}
		//生成提现记录
		coin.setType(1);
		coin.setCreated(DateUtils.getNowDate());
		coin.setStatus(0);
		coin.setName(coin.getName());
		coin.setMobile(coin.getMobile());
		coin.setWechat(coin.getWechat());
		this.coinService.insertHaoqiCoinRecord(coin);
		//修改账户余额
		Member update = new Member();
		update.setId(coin.getMemberId());
		update.setCoin(member.getCoin().subtract(coin.getNum()));
		this.memberService.updateMember(update);
		return R.ok();
	}


	/**
	 * 查询捐赠记录
	 * @param uid
	 * @return
	 */
	@GetMapping("/donation")
	public R donationList(@RequestParam String uid){
		startPage();
		MemberDonation memberDonation = new MemberDonation();
		memberDonation.setMemberId(uid);
		return RFactory.generateR(getDataTable(this.memberDonationService.selectMemberDonationList(memberDonation)));
	}

	/**
	 * 查询捐赠统计
	 * @param uid
	 * @return
	 */
	@GetMapping("/donationcount")
	public R donationCount(@RequestParam String uid){
		HashMap<String, Object> map = new HashMap<>();
		BigDecimal orderSumByType = this.coinService.findOrderSumByType(uid, DateUtils.getNowDate(), 2);
		//查询本月个人订单总额
		BigDecimal sumOrder = this.orderOrderService.findSumCountByUid(uid,1);
		//查询捐赠基数
		ShopConfig shopConfig = this.shopConfigService.findAll().get(0);
		map.put("sumPrice",orderSumByType.add(sumOrder).multiply(shopConfig.getDonation()));
		return RFactory.generateR(map);
	}
}
